package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;

/**
 * 学生数据操作类
 */
public class TeacherDao implements BaseTeacherDao{


    //创建数组
    private static Teacher[] teachers = new Teacher[60];
    static{
        //初始化数据
        Teacher teacher1 = new Teacher("0001", "鹿晗", 28, "1992");
        Teacher teacher2 = new Teacher("0002", "吴亦凡", 30, "1990");
        Teacher teacher3 = new Teacher("0003", "蔡徐坤", 26, "1994");
        Teacher teacher4 = new Teacher("0004", "黄子韬", 25, "1995");
        teachers[0]=teacher1;
        teachers[1]=teacher2;
        teachers[2]=teacher3;
        teachers[3]=teacher4;
    }


    //实现添加学生操作
    public boolean addTeacher(Teacher teacher) {

        //获取需要把数组存放的位置
        int index = -1;
        //遍历数组查看哪里是空的
        for (int i = 0; i < teachers.length; i++) {
            //判断是否为空
            //teachers[i]

            if (teachers[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //返回放不下了
            return false;
        } else {
            //把学生放进去
            teachers[index] = teacher;
            return true;
        }

    }

    public Teacher[] findAllTeachers() {

        return teachers;
    }

    //删除
    public void deleteTeacher(String deleteId) {
        //通过学号查询数组中的位置
        int index = getIndex(deleteId);
        teachers[index] = null;

    }

    //查询学号所在的位置
    public int getIndex(String deleteId) {
        for (int i = 0; i < teachers.length; i++) {
            //stu[i]
            Teacher teacher = teachers[i];
            if (deleteId.equals(teacher.getSid())) {
                return i;
            }
        }
        return -1;
    }

    public void updateTeacher(String updateId, Teacher teacher) {
        int index = getIndex(updateId);
        teachers[index] = teacher;

    }

    public void deleteTeacherByIndex(int index) {
        teachers[index] = null;

    }
}
